package com.laputa.model.manager;

import java.util.*;

import com.laputa.dao.*;
import com.laputa.dao.model.*;
import com.laputa.system.Lib;

public class UserManager{

	private UserDao userDao;
	private CityManager cityManager;
	
	public void setUserDao(UserDao pd){
		this.userDao = pd;
	}
	
	public void setCityManager(CityManager pd){
		this.cityManager = pd;
	}
	
	public void delete(String id) {
		userDao.delete(new Long(id));
	}

	public List getAll() {
		return userDao.getAll();
	}

	public User getUser(String id) {
		return userDao.getUser(new Long(id));
	}

	public User getUser(String name, String password){
		return userDao.getUser(name, password);
	}
	
	public User getUserByName(String name){
		return userDao.getUser(name);
	}
	
	public Long getUserID(String userName){
		return userDao.getUser(userName).getId();
	}
	
	public void save(User p) {
		int groundNum = 8;
		
		City city = new City(0L, (long)groundNum);
		//city.setId(userId);
		cityManager.save(city);
		
		Long cityID = cityManager.getMaxCityID();
		p.setCity(cityID);
		p.setMoney(Lib.defaultUserMoney);
		
		userDao.save(p);
		User user = userDao.getUser(p.getName());
		
		cityManager.createGrounds(user.getId(), groundNum);
	}
	
	public Long getMoney(String id){
		return getUser(id).getMoney();
	}
	
	public void saveMoney(String id, String money){
		User user = getUser(id);
		user.setMoney(new Long(money));
		userDao.save(user);
	}
	
	public boolean validate(String name, String password){
		User u = userDao.getUser(name, password);
		if (u!=null){
			return true;
		}
		else 
			return false;
	}
}
